<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter"
                :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="昵称" prop="nickName">
              <el-input v-model="queryParams.nickName" placeholder="请输入昵称" clearable @keyup.enter="handleQuery"/>
            </el-form-item>
            <el-form-item label="擅长" prop="beGoodAt">
              <el-input v-model="queryParams.beGoodAt" placeholder="请输入擅长" clearable @keyup.enter="handleQuery"/>
            </el-form-item>
            <el-form-item label="真实姓名" prop="realName">
              <el-input v-model="queryParams.realName" placeholder="请输入真实姓名" clearable @keyup.enter="handleQuery"/>
            </el-form-item>
            <el-form-item label="身份证号" prop="idCard">
              <el-input v-model="queryParams.idCard" placeholder="请输入身份证号" clearable @keyup.enter="handleQuery"/>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <!--      <template #header>-->
      <!--        <el-row :gutter="10" class="mb8">-->
      <!--          <el-col :span="1.5">-->
      <!--            <el-button type="primary" plain icon="Plus" @click="handleAdd"-->
      <!--                       v-hasPermi="['app:SkAppUser:add']">新增-->
      <!--            </el-button>-->
      <!--          </el-col>-->
      <!--          <el-col :span="1.5">-->
      <!--            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()"-->
      <!--                       v-hasPermi="['app:SkAppUser:edit']">修改-->
      <!--            </el-button>-->
      <!--          </el-col>-->
      <!--          <el-col :span="1.5">-->
      <!--            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()"-->
      <!--                       v-hasPermi="['app:SkAppUser:remove']">删除-->
      <!--            </el-button>-->
      <!--          </el-col>-->
      <!--          <el-col :span="1.5">-->
      <!--            <el-button type="warning" plain icon="Download" @click="handleExport"-->
      <!--                       v-hasPermi="['app:SkAppUser:export']">导出-->
      <!--            </el-button>-->
      <!--          </el-col>-->
      <!--          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>-->
      <!--        </el-row>-->
      <!--      </template>-->

      <el-table v-loading="loading" :data="SkAppUserList" @selection-change="handleSelectionChange">
        <el-table-column type="expand">
          <template #default="scope">
            <div m="4">
              <p>真实姓名: {{ scope.row.realName }}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;身份证号：{{
                  scope.row.idCard
                }}</p>
              <p m="t-0 b-2">擅长:
                <el-tag style="margin-left: 10px;" v-if="scope.row.beGoodAt"
                        v-for="(item, index) in scope.row.beGoodAt.split(',')" type="primary">{{ item }}
                </el-tag>
              </p>
              <p m="t-0 b-2">个性标签:
                <el-tag style="margin-left: 10px;" v-if="scope.row.personalityTag"
                        v-for="(item, index) in scope.row.personalityTag.split(',')" type="primary">{{ item }}
                </el-tag>
              </p>
              <p>个性签名：{{ scope.row.personalitySign }}</p>
            </div>
          </template>
        </el-table-column>
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="ID" align="center" prop="id" v-if="true"/>
        <el-table-column label="用户类型" align="center" prop="userType"/>
        <el-table-column label="头像" align="center" prop="headImage" width="100">
          <template #default="scope">
            <image-preview :src="scope.row.headImage" :width="50" :height="50"/>
          </template>
        </el-table-column>
        <el-table-column label="昵称" align="center" prop="nickName"/>
        <el-table-column label="性别" align="center" prop="sex"/>
        <el-table-column label="生日" align="center" prop="birthday" width="120"/>
        <el-table-column label="身高" align="center" prop="height"/>
        <el-table-column label="学历" align="center" prop="education"/>
        <el-table-column label="会员等级" align="center" prop="vipType"/>
        <el-table-column label="VIP到期时间" align="center" prop="vipTime" width="120"/>
        <el-table-column label="注册时间" align="center" prop="createTime" width="180"/>
        <el-table-column label="欢迎语" align="center" prop="welcome">
          <template #default="scope">
            <a target="_blank" v-if="scope.row.welcome" :href="scope.row.welcome">
              <VideoPlay style="width: 1em; height: 1em; margin-right: 8px"></VideoPlay>
            </a>
            <div v-else>未录入</div>
          </template>
        </el-table-column>
        <!-- <el-table-column label="真实姓名" align="center" prop="realName" />
        <el-table-column label="身份证号" align="center" prop="idCard" /> -->
        <el-table-column label="用户余额" align="center" prop="price"/>
        <!-- <el-table-column label="密码" align="center" prop="password" />
        <el-table-column label="交易密码" align="center" prop="jyPassword" /> -->
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                         v-hasPermi="['app:SkAppUser:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                         v-hasPermi="['app:SkAppUser:remove']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
                  v-model:limit="queryParams.pageSize" @pagination="getList"/>
    </el-card>
    <!-- 添加或修改app用户对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="900px" append-to-body>
      <el-form ref="SkAppUserFormRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="头像" prop="headImage">
          <image-upload v-model="form.headImage" :limit="1"/>
        </el-form-item>
        <el-form-item label="手机号码" prop="phone">
          <el-input v-model="form.nickName" placeholder="请输入手机号码"/>
        </el-form-item>
        <el-form-item label="昵称" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入昵称"/>
        </el-form-item>
        <el-form-item label="用户类型" prop="userType">
          <el-radio-group v-model="form.userType">
            <el-radio v-for="(item, index) in userType" :key="index" :value="item.value" size="large">{{
                item.label
              }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="生日" prop="birthday">
          <el-input v-model="form.birthday" placeholder="请输入生日"/>
        </el-form-item>
        <el-form-item label="身高" prop="height">
          <el-input v-model="form.height" placeholder="请输入身高">
            <template #append>cm</template>
          </el-input>
        </el-form-item>
        <el-form-item label="学历" prop="education">
          <el-input v-model="form.education" placeholder="请输入学历"/>
        </el-form-item>
        <el-form-item label="擅长" prop="beGoodAt">
          <el-tag v-if="form.beGoodAt" style="margin-right: 10px;" v-for="tag in form.beGoodAt.split(',')" :key="tag"
                  closable :disable-transitions="false" @close="handleClose(tag)">
            {{ tag }}
          </el-tag>
          <el-input v-if="inputVisible" ref="InputRef" v-model="inputValue" class="w-20" style="width: 150px;"
                    ize="small" @keyup.enter="handleInputConfirm" @blur="handleInputConfirm" placeholder="请输入擅长标签"/>
          <el-button v-else class="button-new-tag" size="small" @click="showInput">
            + 新增擅长标签
          </el-button>
        </el-form-item>
        <el-form-item label="个性标签" prop="personalityTag">
          <el-tag v-if="form.personalityTag" style="margin-right: 10px;" v-for="tag in form.personalityTag.split(',')"
                  :key="tag" closable :disable-transitions="false" @close="handleClose2(tag)">
            {{ tag }}
          </el-tag>
          <el-input v-if="inputVisible2" ref="InputRef" v-model="inputValue2" class="w-20" style="width: 150px;"
                    ize="small" @keyup.enter="handleInputConfirm2" @blur="handleInputConfirm2" placeholder="请输入个性标签"/>
          <el-button v-else class="button-new-tag" size="small" @click="showInput2">
            + 新增个性标签
          </el-button>
        </el-form-item>
        <el-form-item label="vip到期时间" prop="vipTime">
          <el-input v-model="form.vipTime" placeholder="vip到期时间"/>
        </el-form-item>
        <el-form-item label="vip等级" prop="vipType">
          <el-radio-group v-model="form.vipType">
            <el-radio v-for="(item, index) in vipTypeList" :key="index" :value="item.value" size="large">{{
                item.label
              }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="个性签名" prop="personalitySign">
          <el-input v-model="form.personalitySign" placeholder="请输入个性签名"/>
        </el-form-item>
        <el-form-item label="真实姓名" prop="realName">
          <el-input v-model="form.realName" placeholder="请输入真实姓名"/>
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="form.idCard" placeholder="请输入身份证号"/>
        </el-form-item>
        <el-form-item label="用户余额" prop="price">
          <el-input v-model="form.price" placeholder="请输入用户余额"/>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码"/>
        </el-form-item>
        <!--        <el-form-item label="交易密码" prop="jyPassword">-->
        <!--          <el-input v-model="form.jyPassword" placeholder="请输入交易密码"/>-->
        <!--        </el-form-item>-->
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="SkAppUser" lang="ts">
import {listSkAppUser, getSkAppUser, delSkAppUser, addSkAppUser, updateSkAppUser} from '@/api/app/SkAppUser';
import {SkAppUserVO, SkAppUserQuery, SkAppUserForm} from '@/api/app/SkAppUser/types';

const {proxy} = getCurrentInstance() as ComponentInternalInstance;

const SkAppUserList = ref<SkAppUserVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const vipTypeList = ref([{
  label: '白银',
  value: 1
}, {
  label: '黄金',
  value: 2
}, {
  label: '钻石',
  value: 3
}]);
const userType = ref([{
  label: '普通用户',
  value: 1
}, {
  label: '达人用户',
  value: 2
}]);
const inputVisible = ref(false);
const inputValue = ref('');

const inputVisible2 = ref(false);
const inputValue2 = ref('');

const InputRef = ref<InstanceType<typeof ElInput>>();
const InputRef2 = ref<InstanceType<typeof ElInput>>();

const queryFormRef = ref<ElFormInstance>();
const SkAppUserFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: SkAppUserForm = {
  id: undefined,
  userType: undefined,
  headImage: undefined,
  nickName: undefined,
  sex: undefined,
  birthday: undefined,
  height: undefined,
  education: undefined,
  beGoodAt: undefined,
  personalityTag: undefined,
  vipTime: undefined,
  personalitySign: undefined,
  vipType: undefined,
  welcome: undefined,
  realName: undefined,
  idCard: undefined,
  price: undefined,
  password: undefined,
  jyPassword: undefined
}
const data = reactive<PageData<SkAppUserForm, SkAppUserQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userType: undefined,
    headImage: undefined,
    nickName: undefined,
    sex: undefined,
    birthday: undefined,
    height: undefined,
    education: undefined,
    beGoodAt: undefined,
    personalityTag: undefined,
    vipTime: undefined,
    personalitySign: undefined,
    vipType: undefined,
    welcome: undefined,
    realName: undefined,
    idCard: undefined,
    price: undefined,
    password: undefined,
    jyPassword: undefined,
    params: {},
    isTgDaren: 1
  },
  rules: {
    id: [
      {required: true, message: "ID不能为空", trigger: "blur"}
    ],
    userType: [
      {required: true, message: "类型1：普通用户 2：达人用户不能为空", trigger: "change"}
    ],
    headImage: [
      {required: true, message: "头像不能为空", trigger: "blur"}
    ],
    nickName: [
      {required: true, message: "昵称不能为空", trigger: "blur"}
    ],
    sex: [
      {required: true, message: "性别不能为空", trigger: "change"}
    ],
    birthday: [
      {required: true, message: "生日不能为空", trigger: "blur"}
    ],
    height: [
      {required: true, message: "身高不能为空", trigger: "blur"}
    ],
    education: [
      {required: true, message: "学历不能为空", trigger: "blur"}
    ],
    beGoodAt: [
      {required: true, message: "擅长不能为空", trigger: "blur"}
    ],
    personalityTag: [
      {required: true, message: "个性标签不能为空", trigger: "blur"}
    ],
    personalitySign: [
      {required: true, message: "个性签名不能为空", trigger: "blur"}
    ],
    realName: [
      {required: true, message: "真实姓名不能为空", trigger: "blur"}
    ],
    idCard: [
      {required: true, message: "身份证号不能为空", trigger: "blur"}
    ],
    price: [
      {required: true, message: "用户余额不能为空", trigger: "blur"}
    ],
    password: [
      {required: true, message: "密码不能为空", trigger: "blur"}
    ],
    // jyPassword: [
    //   {required: true, message: "交易密码不能为空", trigger: "blur"}
    // ]
  }
});

const {queryParams, form, rules} = toRefs(data);

/** 查询app用户列表 */
const getList = async () => {
  loading.value = true;
  const res = await listSkAppUser(queryParams.value);
  SkAppUserList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  SkAppUserFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: SkAppUserVO[]) => {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = "添加app用户";
}

/** 修改按钮操作 */
const handleUpdate = async (row?: SkAppUserVO) => {
  reset();
  const _id = row?.id || ids.value[0]
  const res = await getSkAppUser(_id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = "修改app用户";
}

/** 提交按钮 */
const submitForm = () => {
  SkAppUserFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateSkAppUser(form.value).finally(() => buttonLoading.value = false);
      } else {
        await addSkAppUser(form.value).finally(() => buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess("操作成功");
      dialog.visible = false;
      await getList();
    }
  });
}

/** 删除按钮操作 */
const handleDelete = async (row?: SkAppUserVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除app用户编号为"' + _ids + '"的数据项？').finally(() => loading.value = false);
  await delSkAppUser(_ids);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('app/SkAppUser/export', {
    ...queryParams.value
  }, `SkAppUser_${new Date().getTime()}.xlsx`)
}

const handleClose = (tag: string) => {
  console.log(tag)
  let arr = form.value.beGoodAt.split(',')
  arr.splice(arr.indexOf(tag), 1)
  form.value.beGoodAt = arr.join(',');
}

const handleInputConfirm = () => {
  console.log(form.value.beGoodAt, "form.value.beGoodAtform.value.beGoodAt")
  if (inputValue.value) {
    let arr = [];
    if (form.value.beGoodAt == undefined || form.value.beGoodAt == '') {
      arr = [];
    } else {
      arr = form.value.beGoodAt.split(',')
    }
    arr.push(inputValue.value)
    form.value.beGoodAt = arr.join(',')
  }
  inputVisible.value = false
  inputValue.value = ''
}

const showInput = () => {
  inputVisible.value = true
  nextTick(() => {
    InputRef.value!.input!.focus()
  })
}


const handleClose2 = (tag: string) => {
  console.log(tag)
  let arr = form.value.personalityTag.split(',')
  arr.splice(arr.indexOf(tag), 1)
  form.value.personalityTag = arr.join(',');
}

const handleInputConfirm2 = () => {
  if (inputValue2.value) {
    let arr = [];
    if (form.value.personalityTag == undefined || form.value.personalityTag == '') {
      arr = [];
    } else {
      arr = form.value.personalityTag.split(',')
    }
    arr.push(inputValue2.value)
    form.value.personalityTag = arr.join(',')
  }
  inputVisible2.value = false
  inputValue2.value = ''
}

const showInput2 = () => {
  inputVisible2.value = true
  nextTick(() => {
    InputRef2.value!.input!.focus()
  })
}

onMounted(() => {
  getList();
});
</script>
